package servlet;

import Service.Implement.adminServiceImplement;
import Service.Implement.userServiceImplement;
import Service.Implement.userServiceImplement;
import Service.adminService;
import Service.userService;
import com.alibaba.fastjson.JSON;
import com.example.matchapp.jsonTestServlet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Scope;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import pojo.room;
import returnDataStruct.*;

import javax.annotation.Resource;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

@Controller
public class mainServlet extends HttpServlet  {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        // 设置响应类型:
        response.setContentType("application/json; charset=utf-8");
        response.setCharacterEncoding("UTF-8");
            // 获取操作参数：
            String operation = request.getParameter("operation");
            System.out.println(operation);

     try {
        Method method=this.getClass().getDeclaredMethod(operation,HttpServletRequest.class,HttpServletResponse.class);
        method.invoke(this,request,response);
        }catch (Exception e){
            boolean result = false;
            String userJson = JSON.toJSONString(result);
            OutputStream out = response.getOutputStream();
            out.write(userJson.getBytes("UTF-8"));
            out.flush();
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    private adminService adminServiceGet(){
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("bean1.xml");
        return (adminService) applicationContext.getBean("adminServiceImplement");
    }
    private userService userServiceGet(){
        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("bean1.xml");
        return (userService) applicationContext.getBean("userServiceImplement");
    }




    //管理员登录
    public void ADMIN_LOGIN(HttpServletRequest request,HttpServletResponse response){
        // 设置响应类型:
        response.setContentType("application/json; charset=utf-8");
        response.setCharacterEncoding("UTF-8");
        // 获取参数：
        String password = request.getParameter("password");
        String adminId = request.getParameter("studentId");
        System.out.println(adminId);
        // 调用接口：
        adminService asi= adminServiceGet();
        Boolean success = asi.loginIn(adminId,password);
        System.out.println(success);
        try {
            if(success==false){
                String userJson = JSON.toJSONString(false);
                System.out.println(userJson);
                OutputStream out = response.getOutputStream();
                out.write(userJson.getBytes("UTF-8"));
                out.flush();
            }else{
                adminData result = new adminData(asi.queryAdminByID(adminId));
                String userJson = JSON.toJSONString(result);
                OutputStream out = response.getOutputStream();
                out.write(userJson.getBytes("UTF-8"));
                out.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //管理员注册
    public void REGISTERMANAGER(HttpServletRequest request, HttpServletResponse response){
        // 设置响应类型:
        response.setContentType("application/json; charset=utf-8");
        response.setCharacterEncoding("UTF-8");
        // 获取参数：
        String userName = request.getParameter("userName");
        String studentId = request.getParameter("studentId");
        String password = request.getParameter("password");
        // 调用接口：
        adminService asi=adminServiceGet();
        Boolean result = asi.register(studentId,userName,password)==1;
        String userJson = JSON.toJSONString(result);
        System.out.println(userJson);
        try {
            OutputStream out = response.getOutputStream();
            out.write(userJson.getBytes("UTF-8"));
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }



    //查询所有体育馆
    public void SHOW_LOCATION(HttpServletRequest request,HttpServletResponse response){
        // 设置响应类型:
        response.setContentType("application/json; charset=utf-8");
        response.setCharacterEncoding("UTF-8");
        // 获取参数：
        String studentId = request.getParameter("studentId");
        System.out.println(studentId);
        // 调用接口：
        adminService asi= adminServiceGet();
        System.out.println(studentId);
        locationListData result = new locationListData(asi.queryForStadiums(studentId));
        System.out.println(result);
        String userJson = JSON.toJSONString(result);
        System.out.println(userJson);
        try {
            OutputStream out = response.getOutputStream();
            out.write(userJson.getBytes("UTF-8"));
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //查询体育馆
    public void SEARCH_LOCATION(HttpServletRequest request,HttpServletResponse response){
        // 设置响应类型:
        response.setContentType("application/json; charset=utf-8");
        response.setCharacterEncoding("UTF-8");
        // 获取参数：
        String studentId = request.getParameter("studentId");
        String locationName = request.getParameter("locationName");
        // 调用接口：
        adminService asi= adminServiceGet();
        locationListData result = new locationListData(asi.queryStadiumBYName(locationName));
        String userJson = JSON.toJSONString(result);
        try {
            OutputStream out = response.getOutputStream();
            out.write(userJson.getBytes("UTF-8"));
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    //体育馆详细信息
    public void DETAILED_LOCATION(HttpServletRequest request,HttpServletResponse response){
        // 设置响应类型:
        response.setContentType("application/json; charset=utf-8");
        response.setCharacterEncoding("UTF-8");
        // 获取参数：
        String locationId = request.getParameter("locationId");
        String locationName = request.getParameter("locationName");
        System.out.println(locationId+" "+locationName);
        // 调用接口：
        adminService asi= adminServiceGet();
        locationDetailData result = new locationDetailData(asi.queryStadiumBYName(locationName).get(0),asi.queryForPlaygrounds(Long.parseLong(locationId)));
        String userJson = JSON.toJSONString(result);
        try {
            OutputStream out = response.getOutputStream();
            out.write(userJson.getBytes("UTF-8"));
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    //改变场馆信息
    public void CHANGE_LOCATION(HttpServletRequest request,HttpServletResponse response){
        // 设置响应类型:
        response.setContentType("application/json; charset=utf-8");
        response.setCharacterEncoding("UTF-8");
        // 获取参数：
        String locationId = request.getParameter("location_id");
        String locationName = request.getParameter("location_name");
        String location = request.getParameter("location");
        String location_type_room = request.getParameter("location_type_room");
        String location_type_free = request.getParameter("location_type_free");
        String location_open_time = request.getParameter("location_open_time");
        String location_close_time = request.getParameter("location_close_time");
        String location_sports = request.getParameter("location_sports");
        String location_state = request.getParameter("location_state");
        String studentId = request.getParameter("studentId");
        String category = location_type_room.equals("0")?(location_type_free.equals("0")?"1":"2"):(location_type_free.equals("0")?"3":"4");
        System.out.println(category);
        // 调用接口：
        adminService asi= adminServiceGet();
        boolean result = 1==asi.updateStadium(Long.parseLong(locationId),(asi.queryAdminByID(studentId)).getVx_id(),locationName,location,Integer.parseInt(location_open_time),Integer.parseInt(location_close_time),location_sports,category,location_state);
        String userJson = JSON.toJSONString(result);
        try {
            OutputStream out = response.getOutputStream();
            out.write(userJson.getBytes("UTF-8"));
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    //创建场馆
    public void CREATE_LOCATION(HttpServletRequest request,HttpServletResponse response){
        // 设置响应类型:
        response.setContentType("application/json; charset=utf-8");
        response.setCharacterEncoding("UTF-8");
        // 获取参数：
        String locationName = request.getParameter("location_name");
        System.out.println(locationName);
        String location = request.getParameter("location");
        System.out.println(location);
        String location_type_room = request.getParameter("location_type_room");
        System.out.println(location_type_room);
        String location_type_free = request.getParameter("location_type_free");
        System.out.println(location_type_free);
        String location_open_time = request.getParameter("location_open_time");
        String location_close_time = request.getParameter("location_close_time");
        String location_sports = request.getParameter("location_sports");
        String location_state = request.getParameter("location_state");
        String studentId = request.getParameter("studentId");
        System.out.println(studentId);
        String category = location_type_room.equals("0")?(location_type_free.equals("0")?"1":"2"):(location_type_free.equals("0")?"3":"4");
        System.out.println(category);
        // 调用接口：
        adminService asi= adminServiceGet();
        boolean result = 1==asi.addStadium((asi.queryAdminByID(studentId).getVx_id()),locationName,location,Integer.parseInt(location_open_time),Integer.parseInt(location_close_time),location_sports,category,location_state);
        String userJson = JSON.toJSONString(result);
        try {
            OutputStream out = response.getOutputStream();
            out.write(userJson.getBytes("UTF-8"));
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //查看房间记录
    public void SHOW_ORDERS(HttpServletRequest request,HttpServletResponse response){
        // 设置响应类型:
        response.setContentType("application/json; charset=utf-8");
        response.setCharacterEncoding("UTF-8");
        // 获取参数：
        // 调用接口：
        String studentId = request.getParameter("Id");
        adminService asi= adminServiceGet();
        orderListData result = new orderListData(asi.queryRoom(studentId));
        String userJson = JSON.toJSONString(result);
        try {
            OutputStream out = response.getOutputStream();
            out.write(userJson.getBytes("UTF-8"));
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void SEARCH_ORDER(HttpServletRequest request,HttpServletResponse response)
    {
        // 设置响应类型:
        response.setContentType("application/json; charset=utf-8");
        response.setCharacterEncoding("UTF-8");
        // 获取参数：
        String roomId=request.getParameter("roomId");
        String studentId = request.getParameter("Id");
        // 调用接口：
        System.out.println(studentId+":"+roomId);
        adminService adminService=adminServiceGet();
        List<room> list =new ArrayList<>();
        list.add(adminService.queryRoomById(Long.parseLong(roomId)));
        orderListData result = new orderListData(list);
        String userJson = JSON.toJSONString(result);
        try {
            OutputStream out=response.getOutputStream();
            out.write(userJson.getBytes("UTF-8"));
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    //查询某类房间
    public void SEARCH_ORDERS(HttpServletRequest request,HttpServletResponse response){
        // 设置响应类型:
        response.setContentType("application/json; charset=utf-8");
        response.setCharacterEncoding("UTF-8");
        // 获取参数：
        String searchConditionType = request.getParameter("searchConditionType");
        String searchConditionValue = request.getParameter("searchConditionValue");
        String studentId = request.getParameter("Id");


        // 调用接口：
        adminService asi= adminServiceGet();
        orderListData result;
        String userJson;
        try {
            OutputStream out = response.getOutputStream();
            switch (searchConditionType){
                case "time"://success
                    System.out.println("time:"+searchConditionValue);
                    result = new orderListData(asi.queryRoomByPeriod(searchConditionValue,studentId));
                    userJson = JSON.toJSONString(result);
                    out.write(userJson.getBytes("UTF-8"));
                    out.flush();
                    break;
                case "state"://success
                    result = new orderListData(asi.queryRoomByRoomState(Integer.parseInt(searchConditionValue),studentId));
                    userJson = JSON.toJSONString(result);
                    out.write(userJson.getBytes("UTF-8"));
                    out.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    } 


    //创建场地
    public void CREATE_PLAYGROUND(HttpServletRequest request,HttpServletResponse response){
        // 设置响应类型:
        response.setContentType("application/json; charset=utf-8");
        response.setCharacterEncoding("UTF-8");
        // 获取参数：
        String playgroundName = request.getParameter("name");
        String sportId = request.getParameter("sportsId");
        String prices = request.getParameter("prices");
        String price = request.getParameter("price");
        String display = request.getParameter("state");
        String stadiumId = request.getParameter("stadiumId");
        System.out.println(sportId+" "+price+" "+prices+" "+display+" "+stadiumId);
        // 调用接口：
        adminService asi= adminServiceGet();
        boolean result = 1==asi.addPlayground(playgroundName,sportId,Float.parseFloat(price),display,Long.parseLong(stadiumId));
        String userJson = JSON.toJSONString(result);
        try {
            OutputStream out = response.getOutputStream();
            out.write(userJson.getBytes("UTF-8"));
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    //删除场地
    public void DELETE_PLAYGROUND(HttpServletRequest request,HttpServletResponse response){
        // 设置响应类型:
        response.setContentType("application/json; charset=utf-8");
        response.setCharacterEncoding("UTF-8");
        // 获取参数：
        String stadiumId = request.getParameter("stadiumId");
        String playgroundId = request.getParameter("playgroundId");
        System.out.println(stadiumId+" "+playgroundId);
        // 调用接口：
        adminService asi= adminServiceGet();
        boolean result = 1==asi.deletePlayground(Long.parseLong(playgroundId),Long.parseLong(stadiumId));
        String userJson = JSON.toJSONString(result);
        try {
            OutputStream out = response.getOutputStream();
            out.write(userJson.getBytes("UTF-8"));
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //更新场地
    public void UPDATE_PLAYGROUND(HttpServletRequest request,HttpServletResponse response){
        // 设置响应类型:
        response.setContentType("application/json; charset=utf-8");
        response.setCharacterEncoding("UTF-8");
        // 获取参数：
        String playgroundName = request.getParameter("name");
        System.out.println(playgroundName);
        String sportId = request.getParameter("sportsId");
        System.out.println(sportId);
        String price = request.getParameter("price");
        System.out.println(price);
        String display = request.getParameter("state");
        System.out.println(display);
        String playgroundId = request.getParameter("playgroundId");
        System.out.println(playgroundId);
        // 调用接口：
        adminService asi= adminServiceGet();
        boolean result = 1==asi.updatePlayground(display,playgroundName,Float.parseFloat(price),sportId,Long.parseLong(playgroundId));
        String userJson = JSON.toJSONString(result);
        try {
            OutputStream out = response.getOutputStream();
            out.write(userJson.getBytes("UTF-8"));
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    //用户登陆
    public void LOGIN(HttpServletRequest request, HttpServletResponse response){
        // 设置响应类型:
        response.setContentType("application/json; charset=utf-8");
        response.setCharacterEncoding("UTF-8");
        // 获取参数：
        String password = request.getParameter("password");
        String studentId = request.getParameter("studentId");
        // 调用接口：
        System.out.println(studentId+": "+password);
        userService usi= userServiceGet();
        System.out.println(usi);
        Boolean success = usi.loginIn(studentId,password)==1;
        System.out.println(success);
        System.out.println("loginAfter");
        try {
            if(success==false){
                String userJson = JSON.toJSONString(false);
                System.out.println(userJson);
                OutputStream out = response.getOutputStream();
                out.write(userJson.getBytes("UTF-8"));
                out.flush();
            }else{
                userData result = new userData(usi.queryUserByStudentId(studentId));
                String userJson = JSON.toJSONString(result);
                System.out.println(userJson);
                OutputStream out = response.getOutputStream();
                out.write(userJson.getBytes("UTF-8"));
                out.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //用户注册
    public void REGISTERUSER(HttpServletRequest request,HttpServletResponse response){
        response.setContentType("application/json; charset=utf-8");
        response.setCharacterEncoding("UTF-8");
        // 获取参数：
        String userName = request.getParameter("userName");
        String studentId = request.getParameter("studentId");
        String password = request.getParameter("password");
        // 调用接口：
        userService usi=userServiceGet();
        Boolean result = usi.register(studentId,userName,password)==1;
        String userJson = JSON.toJSONString(result);
        System.out.println(userJson);
        try {
            OutputStream out = response.getOutputStream();
            out.write(userJson.getBytes("UTF-8"));
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //用户改变头像
    public void CHANGE_PROFILE(HttpServletRequest request,HttpServletResponse response){
        response.setContentType("application/json; charset=utf-8");
        response.setCharacterEncoding("UTF-8");
        // 获取参数：
        String userProfile = request.getParameter("userProfile");
        String studentId = request.getParameter("studentId");
        // 调用接口：
        userService usi= userServiceGet();
        Boolean result = usi.updatePhotoUrl(studentId,userProfile)==1;
        String userJson = JSON.toJSONString(result);
        System.out.println(userJson);
        try {
            OutputStream out = response.getOutputStream();
            out.write(userJson.getBytes("UTF-8"));
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //返回前端数据结构
    public void PK_VALUE(HttpServletRequest request,HttpServletResponse response){
        response.setContentType("application/json; charset=utf-8");
        response.setCharacterEncoding("UTF-8");
        // 获取参数：
        String studentId = request.getParameter("studentId");
        // 调用接口：
        userService usi= userServiceGet();
        pkValueData result = new pkValueData(usi.queryInformationByID(usi.queryUserByStudentId(studentId).getUser_id()));

        String userJson = JSON.toJSONString(result);
        try {
            OutputStream out = response.getOutputStream();
            out.write(userJson.getBytes("UTF-8"));
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //用户发送信息
    public void USER_SEND(HttpServletRequest request,HttpServletResponse response){
        // 设置响应类型:
        response.setContentType("application/json; charset=utf-8");
        response.setCharacterEncoding("UTF-8");
        // 获取参数：
        String userId = request.getParameter("userId");
        String roomId = request.getParameter("roomId");
        String content = request.getParameter("content");
        System.out.println(userId+" "+roomId+" "+content);
        // 调用接口：
        userService usi= userServiceGet();
        boolean result = usi.addUserMessage(usi.queryUserByStudentId(userId).getUser_id(),Long.parseLong(roomId),content)==1;
        String userJson = JSON.toJSONString(result);
        try {
            OutputStream out = response.getOutputStream();
            out.write(userJson.getBytes("UTF-8"));
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //用户接受信息
    public void USER_RECEIVE(HttpServletRequest request,HttpServletResponse response){
        // 设置响应类型:
        response.setContentType("application/json; charset=utf-8");
        response.setCharacterEncoding("UTF-8");
        // 获取参数：
        String roomId = request.getParameter("roomId");
        // 调用接口：
        userService usi= userServiceGet();
        messageList result = new messageList(usi.queryMessageByRoomId(Long.parseLong(roomId)));
        System.out.println(result);
        String userJson = JSON.toJSONString(result);
        System.out.println(userJson);
        try {
            OutputStream out = response.getOutputStream();
            out.write(userJson.getBytes("UTF-8"));
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //根据id得到房间列表
    public void GET_PERSON_ROOM(HttpServletRequest request,HttpServletResponse response){
        response.setContentType("application/json; charset=utf-8");
        response.setCharacterEncoding("UTF-8");
        // 获取参数：
        String studentId = request.getParameter("studentId");
        // 调用接口：
        userService usi= userServiceGet();
        System.out.println(usi);
        roomListData result = new roomListData(usi.queryUserOrder(usi.queryUserByStudentId(studentId).getUser_id()));
        System.out.println(result);
        String userJson = JSON.toJSONString(result);
        try {
            OutputStream out = response.getOutputStream();
            out.write(userJson.getBytes("UTF-8"));
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //得到体育馆列表
    public void GET_LOCATIONARRAY(HttpServletRequest request,HttpServletResponse response){
        // 设置响应类型:
        response.setContentType("application/json; charset=utf-8");
        response.setCharacterEncoding("UTF-8");
        // 获取参数：
        String sportId = request.getParameter("sportsNum");
        // 调用接口
        userService usi= userServiceGet();
        locationNameArray result = new locationNameArray(usi.queryStadiumsByCategory(sportId));
        String userJson = JSON.toJSONString(result);
        try {
            OutputStream out = response.getOutputStream();
            out.write(userJson.getBytes("UTF-8"));
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    //得到场地列表
    public void GET_PLAYGROUNDARRAY(HttpServletRequest request,HttpServletResponse response){
        // 设置响应类型:
        response.setContentType("application/json; charset=utf-8");
        response.setCharacterEncoding("UTF-8");
        // 获取参数：
        String sportId = request.getParameter("sportsNum");
        String stadium = request.getParameter("locationsNum");
        // 调用接口：
        userService usi= userServiceGet();
        playgroundNameArray result = new playgroundNameArray(usi.queryPlaygroundByStadiumName(stadium,sportId));
        System.out.println(result);
        String userJson = JSON.toJSONString(result);
        try {
            OutputStream out = response.getOutputStream();
            out.write(userJson.getBytes("UTF-8"));
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    //得到时间列表
    public void GET_TIMEARRAY(HttpServletRequest request,HttpServletResponse response){
        // 设置响应类型:
        response.setContentType("application/json; charset=utf-8");
        response.setCharacterEncoding("UTF-8");
        // 获取参数：
        String locationNum = request.getParameter("locationNum");
        String date = request.getParameter("date");
        String playgroundNum = request.getParameter("playgroundsNum");
        // 调用接口
        userService usi= userServiceGet();
        System.out.println(locationNum+" "+date+" "+playgroundNum);

        timeArray result = new timeArray(usi.queryLeisureTime(playgroundNum,locationNum,date));
        System.out.println(result);
        System.out.println("Got timeArray");
        String userJson = JSON.toJSONString(result);
        System.out.println(userJson);
        try {
            OutputStream out = response.getOutputStream();
            out.write(userJson.getBytes("UTF-8"));
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //创建房建
    public void CREATE_ROOM(HttpServletRequest request,HttpServletResponse response){
        // 设置响应类型:
        response.setContentType("application/json; charset=utf-8");
        response.setCharacterEncoding("UTF-8");
        // 获取参数：
        String hostId = request.getParameter("hostId");
        String capacity = request.getParameter("personNum");
        String sportId = request.getParameter("sportsNum");
        String date = request.getParameter("date");
        String stadium = request.getParameter("locationsNum");
        String playgroundName = request.getParameter("playgroundsNum");
        String beginTime = request.getParameter("starttime");
        String endTime = request.getParameter("endtime");
        System.out.println(hostId+" "+capacity+" "+sportId+" "+date+" "+stadium+" "+playgroundName+" "+beginTime+" "+endTime);
        // 调用接口：
        userService usi= userServiceGet();
        Long result = usi.createRoom(usi.queryUserByStudentId(hostId).getUser_id(),Integer.parseInt(capacity),sportId,date,stadium,playgroundName,Integer.parseInt(beginTime),Integer.parseInt(endTime));
        String userJson = JSON.toJSONString(result);
        try {
            OutputStream out = response.getOutputStream();
            out.write(userJson.getBytes("UTF-8"));
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //查询房间信息
    public void SHOW_ROOM(HttpServletRequest request,HttpServletResponse response){
        // 设置响应类型:
        response.setContentType("application/json; charset=utf-8");
        response.setCharacterEncoding("UTF-8");
        // 获取参数：
        String sportId = request.getParameter("sportsNum");
        // 调用接口：
        userService usi= userServiceGet();
        roomListData result;
        if(sportId.equals("0"))
            result = new roomListData(usi.queryMatchingRoom());
        else
         result = new roomListData(usi.queryMatchingRoomByCategory(sportId));
        String userJson = JSON.toJSONString(result);
        System.out.println(userJson);
        try {

            OutputStream out = response.getOutputStream();
            System.out.println(out);
            out.write(userJson.getBytes("UTF-8"));
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //查询房间
    public void SEARCH_ROOM(HttpServletRequest request,HttpServletResponse response){
        // 设置响应类型:
        response.setContentType("application/json; charset=utf-8");
        response.setCharacterEncoding("UTF-8");
        // 获取参数：
        String roomId = request.getParameter("roomId");
        // 调用接口：
        userService usi= userServiceGet();
        List<room> lr = new ArrayList<>();
        lr.add(usi.queryRoomByRoomID(Long.parseLong(roomId)));
        roomListData result = new roomListData(lr);
        String userJson = JSON.toJSONString(result);
        try {
            OutputStream out = response.getOutputStream();
            out.write(userJson.getBytes("UTF-8"));
            out.flush();
            System.out.println("发送完毕");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //查询房间详细信息
    public void DETAILED_ROOM(HttpServletRequest request,HttpServletResponse response){
        // 设置响应类型:
        response.setContentType("application/json; charset=utf-8");
        response.setCharacterEncoding("UTF-8");
        // 获取参数：
        String roomId = request.getParameter("roomId");
        // 调用接口：
        userService usi= userServiceGet();
        roomDetailData result = new roomDetailData(usi.queryRoomByRoomID(Long.parseLong(roomId)),usi.LookOtherUser(Long.parseLong(roomId)));
        String userJson = JSON.toJSONString(result);
        try {
            OutputStream out = response.getOutputStream();
            out.write(userJson.getBytes("UTF-8"));
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //加入房间
    public void JOIN_ROOM(HttpServletRequest request,HttpServletResponse response){
        // 设置响应类型:
        response.setContentType("application/json; charset=utf-8");
        response.setCharacterEncoding("UTF-8");
        // 获取参数：
        String sportId = request.getParameter("sportsNum");
        String roomId = request.getParameter("roomId");
        String studentId = request.getParameter("studentId");
        System.out.println(sportId+" "+roomId+" "+studentId);
        // 调用接口：
        userService usi= userServiceGet();
        Boolean result = usi.joinRoom((usi.queryUserByStudentId(studentId)).getUser_id(),sportId,Long.parseLong(roomId))==1;
        String userJson = JSON.toJSONString(result);
        try {
            OutputStream out = response.getOutputStream();
            out.write(userJson.getBytes("UTF-8"));
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //退出房间
    public void QUIT_ROOM(HttpServletRequest request,HttpServletResponse response){
        // 设置响应类型:
        response.setContentType("application/json; charset=utf-8");
        response.setCharacterEncoding("UTF-8");
        // 获取参数：
        String roomId = request.getParameter("roomId");
        String studentId = request.getParameter("studentId");
        System.out.println(roomId+" "+studentId);
        // 调用接口：
        userService usi= userServiceGet();
        Boolean result = usi.exitRoom(Long.parseLong(roomId),usi.queryUserByStudentId(studentId).getUser_id())==1;
        String userJson = JSON.toJSONString(result);
        System.out.println(userJson);
        try {
            OutputStream out = response.getOutputStream();
            out.write(userJson.getBytes("UTF-8"));
            out.flush();
            System.out.println("数据发送发完");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //删除房间
    public void DELETE_ROOM(HttpServletRequest request,HttpServletResponse response){
        // 设置响应类型:
        response.setContentType("application/json; charset=utf-8");
        response.setCharacterEncoding("UTF-8");
        // 获取参数：
        String roomId = request.getParameter("roomId");
        // 调用接口：
        userService usi= userServiceGet();
        Boolean result = usi.deleteRoom(Long.parseLong(roomId))==1;
        String userJson = JSON.toJSONString(result);
        System.out.println(userJson);
        try {
            OutputStream out = response.getOutputStream();
            out.write(userJson.getBytes("UTF-8"));
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }



}
